package com.ljy.my_study.lintcode.相似字符串组;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author James
 * @date 2018年5月23日
 */
public class Solution {
	public static void main(String[] args) {
		Solution s=new Solution();
//		int count=s.numSimilarGroups2(new String[]{"blw","bwl","wlb"});
//		int count=s.numSimilarGroups2(new String[]{"tars","rats","arts","star"});
//		int count=s.numSimilarGroups2(new String[]{"omv","ovm"});
		int count=s.numSimilarGroups2(new String[]{"ajdidocuyh","djdyaohuic","ddjyhuicoa","djdhaoyuic","ddjoiuycha","ddhoiuycja","ajdydocuih","ddjiouycha","ajdydohuic","ddjyouicha"});
		System.out.println(count);
	}
	
	public int numSimilarGroups2(String[] A) {
		int[] Arr=new int[A.length];
		int sum=0;
		int count=0;
		int arrGroupCount=1;
		List<Set<Integer>> list=new ArrayList<>();
		for(int i=0;i<A.length;i++) {
			for(int j=0;j<A.length;j++) {
				if(compareTwoStrSimilar(A[i],A[j])) {
					//System.out.println(A[i]+","+A[j]);
					int iVal=Arr[i];
					int jVal=Arr[j];
					if(iVal>0&&jVal==0) {
						Arr[j]=iVal;
					}else if(iVal==0&&jVal>0) {
						Arr[i]=jVal;
					}else if(iVal==0&&jVal==0){
						Arr[i]=Arr[j]=arrGroupCount;
						arrGroupCount++;
					}else {
						for(int k=0;k<Arr.length;k++) {
							if(Arr[k]==Arr[i]) {
								Arr[k]=Arr[j];
							}
						}
						
					}
					count++;
				}
			}
			if(count>sum) {
				sum=count;
			}
			count=0;
		}
		//System.out.println(arrGroupCount);
		System.out.println(Arrays.toString(Arr));
		Set<Integer> set=new HashSet<>();
		int zoreCount=0;
		for(int i=0;i<Arr.length;i++) {
			if(Arr[i]==0) {
				zoreCount++;
				continue;
			}
			set.add(Integer.valueOf(Arr[i]));
		}
		return set.size()+zoreCount;
	}
	
	public boolean compareTwoStrSimilar(String a,String b) {
		if(a.length()!=b.length()) {
			return false;
		}
		char[] aCharArr=a.toCharArray();
		char[] bCharArr=b.toCharArray();
		int count=0;
		char aOneChar = 1;
		char aTwoChar = 2;
		char bOneChar = 3;
		char bTwoChar = 4;
		for(int i=0;i<aCharArr.length;i++) {
			if(aCharArr[i]!=bCharArr[i]) {
				count++;
				if(count==1) {
					aOneChar=aCharArr[i];
					bOneChar=bCharArr[i];
				}else {
					aTwoChar=aCharArr[i];
					bTwoChar=bCharArr[i];
				}
			}
			if(count>2) {
				return false;
			}
		}
		if(count!=2) {
			return false;
		}
		if(aOneChar!=bTwoChar||aTwoChar!=bOneChar) {
			return false;
		}
		return true;
	}
}
